package com.thxy.jxgl.mubiaoka.manager;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.thxy.jxgl.db.DB;
import com.thxy.jxgl.filter.ToCh;
import com.thxy.jxgl.javaBean.khzb;

public class ZhibiaoManager {
	private static ZhibiaoManager instance = new ZhibiaoManager();

	private ZhibiaoManager() {
	}

	public static ZhibiaoManager getInstance() {
		return instance;
	}
	public List getZhibiao() {
		List zblist = new ArrayList();
		Connection conn = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		String sql = "select * from t_zb";
		try {
			conn = DB.getConn();
			pstm = conn.prepareStatement(sql);
			rs = pstm.executeQuery();
			while (rs.next()) {
				khzb zb = new khzb();
				zb.setZb_id(rs.getString("zb_id"));
				zb.setZb_name(rs.getString("zb_name"));
				zb.setZb_type(rs.getString("zb_type"));

				zblist.add(zb);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DB.close(rs);
			DB.close(pstm);
			DB.close(conn);
		}
		return zblist;
	}
	public boolean addZhibiao(khzb zb){
		Connection conn=null;
		PreparedStatement pstm=null;
		String sql="INSERT INTO t_zb (zb_id,zb_name,zb_type) VALUES(?,?,?)";
		try {
			conn = DB.getConn();
			pstm = conn.prepareStatement(sql);
			pstm.setString(1, zb.getZb_id());
			pstm.setString(2, ToCh.toChinese(zb.getZb_name()));
			pstm.setString(3, zb.getZb_type());
			return pstm.execute();	
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}finally{				
			DB.close(pstm);
			DB.close(conn);
		}
	}
	public boolean updateZhibiao(String zb_name, String zb_type,String zb_id){
		Connection conn=null;
		PreparedStatement pstm=null;
		String sql="update t_zb set zb_name=?,zb_type=? where zb_id=?;";
		try {
			conn = DB.getConn();
			pstm = conn.prepareStatement(sql);
			pstm.setString(1, zb_name);
			pstm.setString(2, zb_type);
			pstm.setString(3,zb_id);
			return pstm.execute();	
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}finally{				
			DB.close(pstm);
			DB.close(conn);
		}
	}

	public boolean deleZhibiao(String zbno) {
		Connection conn = null;
		PreparedStatement pstm = null;
		String sql = "DELETE FROM t_zb WHERE zb_id=?";
		try {
			conn = DB.getConn();
			pstm = conn.prepareStatement(sql);
			pstm.setString(1, zbno);
			return pstm.execute();
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		} finally {
			DB.close(pstm);
			DB.close(conn);
		}
	}

}
